package com.mixfate.nacos;

import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.alibaba.nacos.spring.context.annotation.config.EnableNacosConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@EnableScheduling
@SpringBootApplication
@EnableNacosConfig
//@NacosPropertySource(dataId = "nacos-config",groupId = "dev",autoRefreshed = true,type = ConfigType.YAML)
public class NacosConfigApplication {

    public static void main(String[] args) {
        SpringApplication.run(NacosConfigApplication.class, args);
    }

}

@Slf4j
@Component
class Task {

    //在nacos配置文件中配置app.name并实时修改观察变化,注意autoRefreshed默认为false
    @NacosValue(value = "${app.name:default}", autoRefreshed = true)
    private String name;

    //模拟每秒输出配置中心的配置项,动态修改后即可看到修改的结果
    @Scheduled(cron = "* * * * * ?")
    public void test() {
        log.info("app.name = {}", name);
    }

}
